package com.yonyou.brigade.cron.order.ctripBC;

import com.alibaba.fastjson.JSONObject;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.order_center.model.BriHotelOrder;
import com.yonyou.brigade.order_center.model.CnvFlightOrderDO;
import com.yonyou.brigade.order_center.model.CnvHotelOrderDO;
import com.yonyou.brigade.order_center.model.CnvTrainOrderDO;
import com.yonyou.brigade.order_center.service.IBriFlightOrderService;
import com.yonyou.brigade.order_center.service.IBriHotelOrderService;
import com.yonyou.brigade.order_center.service.IBriTrainOrderService;
import com.yonyou.brigade.third_parties.ctripBC.model.CtripBCConfig;
import com.yonyou.brigade.third_parties.ctripBC.service.ICtripBCOrderService;
import com.yonyou.brigade.third_parties.smectrip.service.ICtripSmeOrderService;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.ybzboot.starter.elasticjob.lite.annotation.ElasticJobConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 定时更新已完成订单
 * </p>
 *
 * @author kangfw5
 * @since 2019-02-26
 */
@ElasticJobConfig(cron = "0 0 3 * * ? ", shardingTotalCount = 1)
@Component
public class UpdateCtripBCOrders implements SimpleJob {
    private final static Logger logger = LoggerFactory.getLogger(UpdateCtripBCOrders.class);

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("briFlightOrderServiceImpl")
    private IBriFlightOrderService briFlightOrderService;

    @Autowired
    @Qualifier("briHotelOrderServiceImpl")
    private IBriHotelOrderService briHotelOrderService;

    @Autowired
    @Qualifier("briTrainOrderServiceImpl")
    private IBriTrainOrderService briTrainOrderService;

    @Autowired
    private ICtripBCOrderService ctripBCOrderService;

    @Autowired
    private ICtripSmeOrderService ctripSmeOrderService;

    private static final String DATEFORMAT = "yyyy-MM-dd";


    @Override
    public void execute(ShardingContext shardingContext) {
        List<BriChannel> briChannels = briChannelService.getEnableChannelByEnable();
        if (briChannels != null && briChannels.size() > 0) {

            SimpleDateFormat sf = new SimpleDateFormat(DATEFORMAT);
            Date yesterday = null;
            try {
                yesterday = sf.parse(DateUtils.getCurrentDateMinusOneDay(DATEFORMAT));
            } catch (ParseException e) {
                logger.error("日期转换错误！" + e.getMessage());
            }

            for (BriChannel briChannel : briChannels) {
                String channel = briChannel.getChannel();
                String tenant = briChannel.getTenant();
                if (ChannelEnum.CTRIPBC.getName().equals(channel)) {
                    Map<String, String> params = new HashMap<>();
                    params.put("tenant", tenant);
                    CtripBCConfig ctripBCConfig = JSONObject.parseObject(briChannel.getConfig(), CtripBCConfig.class);
                    if ("1".equals(ctripBCConfig.getOrderGetMod())) {
                        continue;
                    }
                    params.put("from_type", ChannelEnum.CTRIPBC.getName());

                    // 获取需要更新的订单号
                    List<CnvFlightOrderDO> flightOrderDOs = briFlightOrderService.getCnvFlightOrderDos(params, yesterday, yesterday);
                    List<BriHotelOrder> briHotelOrderList = briHotelOrderService.getBriHotelOrderByDepartureDate(params, yesterday, yesterday);
                    List<CnvTrainOrderDO> trainOrderDOs = briTrainOrderService.getCnvTrainOrderDos(params, yesterday, yesterday);
                    List<String> orderIdList = new ArrayList<>();
                    for (CnvFlightOrderDO cnvFlightOrderDO : flightOrderDOs) {
                        orderIdList.add(cnvFlightOrderDO.getOrderNo());
                    }
                    for (BriHotelOrder briHotelOrder : briHotelOrderList) {
                        orderIdList.add(briHotelOrder.getOrderNo());
                    }
                    for (CnvTrainOrderDO cnvTrainOrderDO : trainOrderDOs) {
                        orderIdList.add(cnvTrainOrderDO.getOrderNo());
                    }
                    ctripBCOrderService.getCtripBCOrdersByOrderId("1", orderIdList, tenant, briChannel.getId(), ctripBCConfig);

                } else if (ChannelEnum.CTRIPSME.getName().equals(channel)) {
                    Map<String, String> params = new HashMap<>();
                    params.put("tenant", tenant);
                    params.put("from_type", ChannelEnum.CTRIPSME.getName());
                    // 获取需要更新的订单号
                    List<CnvFlightOrderDO> flightOrderDOs = briFlightOrderService.getCnvFlightOrderDos(params, yesterday, yesterday);
                    List<CnvHotelOrderDO> hotelOrderDOS = briHotelOrderService.getCnvHotelOrderDos(params, yesterday, yesterday);
                    List<CnvTrainOrderDO> trainOrderDOs = briTrainOrderService.getCnvTrainOrderDos(params, yesterday, yesterday);
                    List<String> orderIdList = new ArrayList<>();
                    for (CnvFlightOrderDO cnvFlightOrderDO : flightOrderDOs) {
                        orderIdList.add(cnvFlightOrderDO.getOrderNo());
                    }
                    for (CnvHotelOrderDO cnvHotelOrderDO : hotelOrderDOS) {
                        orderIdList.add(cnvHotelOrderDO.getOrderNo());
                    }
                    for (CnvTrainOrderDO cnvTrainOrderDO : trainOrderDOs) {
                        orderIdList.add(cnvTrainOrderDO.getOrderNo());
                    }
                    ctripSmeOrderService.getCtripSmeOrderListByOrderId("1", orderIdList, tenant);
                }
            }
        }
    }
}
